const db = require("../../db/sql");
const {postDao} = require("../../DAO/postData");
const knex = require("../../db/knex");

async function deleteUser(params, res) {
    let id = params.userId;
    await knex.transaction(async trx => {
        let search = await trx('userlist').select('*').where({userId: id})
        if (search[0].role == '系统管理员') {
            res.sendStatus(409)
            return 0
        }
    })
    const sql = 'DELETE FROM userlist WHERE userId = ?';
    db.query(sql, id, (error, results) => {
        if (error) {
            console.error(error);
            res.status(500).send({
                error: '服务器内部错误'
            });
        } else if (results.affectedRows === 0) {
            res.status(404).send({
                error: `找不到 ID 为 ${id} 的用户`
            });
        } else {
            res.send({
                message: '用户信息已成功删除'
            });
        }
    });
}

module.exports = {
    deleteUser
}
